import { httpErrors } from "@/config/httpErrors";
import axios, { AxiosError } from "axios";
import { message } from "antd";
export const useAxios = () => {
  const http = axios.create({
    baseURL: "http://localhost:3000",
    timeout: 1000,
    headers: { "X-Custom-Header": "foobar" },
  });
  http.interceptors.request.use(
    function (config) {
      return config;
    },
    function (error) {
      return Promise.reject(error);
    }
  );

  // 添加响应拦截器
  http.interceptors.response.use(
    function (response) {
      return response;
    },
    function (error: AxiosError) {
      const errMessage =
        httpErrors[error.status as keyof typeof httpErrors] || "请求失败";
      message.info({ content: errMessage});
      return Promise.reject(error);
    }
  );
  return { http };
};
